package com.zp.security.jwt.handler;

import com.zp.common.base.resp.BaseResp;
import com.zp.common.base.utils.ResponseUtils;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录失败的处理器
 * AbstractAuthenticationProcessingFilter调用unsuccessfulAuthentication方法
 *
 * @author zp
 */
@Component
public class LoginAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        if (exception instanceof BadCredentialsException){
            ResponseUtils.result(response,new BaseResp<>("501","用户名密码不正确"));
        }
        ResponseUtils.result(response,new BaseResp("502","登录失败"));
    }
}
